package com.example.mobile203zlw20216061300525;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private String url="http://121.9.253.237:8888/traffic/api/v2/user_login";
    private TextView textView;
    private EditText edUser;
    private EditText edPassword;
    private RadioGroup rdGroup;
    private CheckBox checkBox;
    private CheckBox checkBox2;
    private Button btSinger;
    private Button btRegin;
    OkHttpClient okHttp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edUser = (EditText) findViewById(R.id.ed_user);
        edPassword = (EditText) findViewById(R.id.ed_password);
        rdGroup = (RadioGroup) findViewById(R.id.rd_group);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        btSinger = (Button) findViewById(R.id.bt_singer);
        btRegin = (Button) findViewById(R.id.bt_regin);
    }

    public void denlu(View view) {
        okHttp=new OkHttpClient();
        String user=edUser.getText().toString();
        String password=edPassword.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                JsonObject jsonObject=new JsonObject();
                jsonObject.addProperty("UserName",user);
                jsonObject.addProperty("UserPWd",password);
                String json=jsonObject.toString();
                MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
                RequestBody requestBody=RequestBody.create(json,mediaType);
                Request request=new Request.Builder().url(url).build();
                Call call=okHttp.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        Gson gson=new Gson();
                        LoginBean loginBean=gson.fromJson(string,LoginBean.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (loginBean.getERRMSG().equals("F")){
                                    new AlertDialog.Builder(MainActivity.this).
                                    setMessage("登录成功")
                                     .create()
                                     .show();
                                }else if (loginBean.getRESULT().equals("S")){
                                    new AlertDialog.Builder(MainActivity.this).
                                            setMessage("登录失败")
                                            .create()
                                            .show();
                                }
                            }
                        });
                    }
                });
            }
        }).start();
    }
}